﻿Public Class XuatChiTietDA
    Private cn As DatabaseConnect
    Private sql As String

    Public Sub New()
        cn = New DatabaseConnect()
    End Sub

    Public Function InXuatCT(ByVal xct As XuatChiTietDT)
        If xct.MaKH <> "" Then
            sql = "INSERT INTO SanPham_Xuat_ChiTiet VALUES('" + xct.Ma + "'," & xct.STT & ", " & xct.MaLoai & ", " & xct.SoLuong & ", '" + xct.MaKH + "', N'" + xct.GhiChu + "', " & xct.ThanhTien & ")"
        Else
            sql = "INSERT INTO SanPham_Xuat_ChiTiet(MaXuat,STT,MaLoai,soluong,ghichu,thanhtien) VALUES('" + xct.Ma + "'," & xct.STT & ", " & xct.MaLoai & ", " & xct.SoLuong & ", N'" + xct.GhiChu + "', " & xct.ThanhTien & ")"
        End If

        Return cn.Execute(sql)
    End Function

    Public Function UpXuatCT(ByVal xct As XuatChiTietDT)
        sql = "UPDATE SanPham_Xuat_ChiTiet SET MaKH = '" + xct.MaKH + "', ghichu = N'" + xct.GhiChu + "' WHERE MaXuat = '" + xct.Ma + "', STT = " & xct.STT
        Return cn.Execute(sql)
    End Function

    Public Function GetData(ByVal dieukien As String) As List(Of XuatChiTietDT)
        Dim xct As XuatChiTietDT
        Dim list As New List(Of XuatChiTietDT)
        Dim dt As New DataTable
        dt = cn.GetData("SELECT * FROM SanPham_Xuat_ChiTiet" + dieukien)

        For i As Integer = 0 To dt.Rows.Count() - 1
            Dim ma As String = dt.Rows(i)("MaXuat").ToString()
            Dim stt As Integer = Convert.ToInt32(dt.Rows(i)("STT"))
            Dim maloai As Integer = Convert.ToInt32(dt.Rows(i)("MaLoai"))
            Dim soluong As Double = Convert.ToDouble(dt.Rows(i)("soluong"))
            Dim ghichu As String = dt.Rows(i)("ghichu").ToString()
            Dim kh As String = dt.Rows(i)("MaKH").ToString()
            Dim thanhtien As Double = Convert.ToDouble(dt.Rows(i)("thanhtien"))
            '-----'
            xct = New XuatChiTietDT(ma, maloai, stt, soluong, kh, ghichu, thanhtien)
            list.Add(xct)
        Next
        Return list
    End Function

    Public Function TinhTongSoLuong(ByVal ma As String, ByVal loai As Integer) As Double
        Dim dt As New DataTable
        dt = cn.GetData("SELECT SUM(soluong) as Tong FROM SanPham_Xuat_ChiTiet WHERE MaXuat = '" + ma + "' AND MaLoai = " & loai)
        Dim tong As Double = Convert.ToDouble(dt.Rows(0)("Tong"))
        Return tong
    End Function

    Public Function GetSTT(ByVal ma As String) As Integer
        Dim stt As Integer = 1
        Dim dt As New DataTable
        dt = cn.GetData("SELECT * FROM SanPham_Xuat_ChiTiet WHERE MaXuat = '" + ma + "'")
        If dt.Rows.Count > 0 Then
            stt = dt.Rows.Count + 1
        End If
        Return stt
    End Function
End Class
